//
//  SelectIndustyViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/5.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "SelectIndustyViewController.h"
#import "SelectIndustyTableViewCell.h"
#import "AppTools.h"
@interface SelectIndustyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSIndexPath* lastIndexPath;
@end

@implementation SelectIndustyViewController

-(NSMutableArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优选行业";
    self.view.backgroundColor = ETColorWithRGB(242, 244, 246);
    //创建顶部分割线
    [self setupView];
    //创建tableView
    [self createTableView];
}
-(void)setupView
{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = ETTableViewLineColor;
    lineView.frame = CGRectMake(0, 0, ETScreenW, ETTableViewLineHeight);
    [self.view addSubview:lineView];
}
-(void)createTableView
{
    self.titleArray = @[@"智能推荐",@"便利生活",@"家居礼品",@"餐饮行业",@"珠宝首饰",@"汽车美容",@"票务加油"].mutableCopy;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 1, ETScreenW, ETScreenH - 64 - 1) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = ETColorWithRGB(242, 244, 246);
    //禁止滑动
//    self.tableView.scrollEnabled = NO;
    //隐藏分割线
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[SelectIndustyTableViewCell class] forCellReuseIdentifier:@"cell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectIndustyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.lastIndexPath = [AppTools shareInstance].selectIndustyValue;
    if (self.lastIndexPath != nil && self.lastIndexPath == indexPath) {
        if (self.selectIndustyBlock) {
            self.selectIndustyBlock(cell.titleLab.text);
        }
        cell.imv.image = [UIImage imageNamed:@"list_btn_choose"];
    }else
    {
        cell.imv.image = nil;
    }
    //默认选择第一行
    if (indexPath.row == 0 && self.lastIndexPath == nil) {
        cell.imv.image = [UIImage imageNamed:@"list_btn_choose"];
    }
    //最后一条分割线不显示
    if (indexPath.row == self.titleArray.count - 1) {
        cell.lineView.hidden = YES;
    }
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //记录选中
    [AppTools shareInstance].selectIndustyValue = indexPath;
    [self.tableView reloadData];
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
